<template>
    <div class="userForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="新增赛事记录" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
        <!--图片上传 -->
       <!-- <el-form   label-width="120px"  ref="addUserUploadForm">
          &lt;!&ndash; 代码中的1表示第几个 和引入js的type变量对应 &ndash;&gt;
          <el-form-item label="头像">
            <el-upload
              class="upload"
              :action="getUploadUrl"
              list-type="picture-card"
              :limit="1"
              accept="image/*"
              :on-change="(file,fileList)=>{imgHandleChange(file,fileList,1,this,'addUserUploadForm','user')}"
              :on-remove="(file,fileList)=>{imgHandleRemove(1,this)}"
              :on-before="(file,fileList)=>{imghandleBeforeUpload(file,1,this)}"
              :auto-upload="false"
              :file-list="fileList[0]"
              ref="file1">
              <i class="el-icon-plus"></i>
            </el-upload>
            &lt;!&ndash;<viewer-dialog ref="viewer" :images="imagesUp"></viewer-dialog>&ndash;&gt;
          </el-form-item>
        </el-form>-->
        <!--图片上传 结束 -->

      <el-form :model="eventRecords" label-width="80px"  :rules="addEventRecordsRules" ref="addEventRecordsForm">
        <el-form-item label="球队数量" prop="notnum" >
          <el-col :span="16">
           <el-input v-model="eventRecords.notnum" placeholder="请填写参赛球队数量" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="类型编号" prop="mtno">
          <el-col :span="16">
            <el-input v-model="eventRecords.mtno" placeholder="请填入比赛类型编号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="赛事名称" prop="ename">
          <el-col :span="16">
            <el-input v-model="eventRecords.ename" placeholder="请填入赛事名称" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="开始日期">
          <el-col :span="11">
            <el-date-picker  placeholder="选择日期" v-model="eventRecords.stime" style="width: 100%;"
                             type="date"
                             format="yyyy-MM-dd"
                             value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="结束日期">
          <el-col :span="11">
            <el-date-picker  placeholder="选择日期" v-model="eventRecords.etime" style="width: 100%;"
                             type="date"
                             format="yyyy-MM-dd"
                             value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="举办年份">
          <el-col :span="11">
            <el-date-picker  placeholder="选择年份" v-model="eventRecords.year" style="width: 100%;"
                             type="year"
                             format="yyyy"
                             value-format="yyyy"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="举办地点" prop="venue">
          <el-col :span="16">
            <el-input v-model="eventRecords.venue" placeholder="请填入举办地点" clearable/>
          </el-col>
        </el-form-item>
        <!--<el-form-item label="状态">
          <el-col :span="3">
            <el-select v-model="eventRecords.ustate" placeholder="请选择">
              <el-option v-for="(usergroup,index) in allUserGroup" :key="usergroup.num" :label="usergroup.userName" :value="usergroup.userName"/>
            </el-select>
          </el-col>
        </el-form-item>-->
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="addEventRecords('addEventRecordsForm')">添加</el-button></span>
        </el-col>

    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import utils from "../../../../static/js/utils/upload.js"
  import { regionData, CodeToText } from "element-china-area-data";
    export default {
        name: "addEventRecords",
        data(){
          return {
            //行政区使用
            options: regionData,
            uaddr: [],
            value: new Date(),
            //行政区使用结束
            eventRecords:{notnum:"",mtno:"",ename:"",stime:"",etime:"",year:"",venue:""},
              checkAll: false,
              isIndeterminate: true,
              allUserGroup:[{num:0,"userName":"在线"},{num:1,"userName":"离线"}],
            addEventRecordsRules:{},
              addDialogIsShow:false,

            fileList: [[],[],[]], //缓存区文件
            uploadFile:[[],[],[]], //  上传用文件
            formData:{files:[]},
            imagesUp:null,
            //上传使用结束
          }
        },
      methods: {
         validateName:function(rule,value,callback){
           console.log(rule);
           if(value==1) {
              callback(new Error("1不正常"));
           }else{
             callback();
           }
  },
        init() {
          this.addDialogIsShow = true;
        },
        handleCheckAllChange(val) {
          this.user.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },

        addEventRecords:function(addEventRecordsForm){
          this.$refs[addEventRecordsForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.eventRecords);

              this.setDataFromAxios();
              this.handleClose();

            } else {
              console.log('error submit!!')
              return false
            }
          });

        },
        updateView(e) {
          this.$forceUpdate();
        },

        //行政区使用
        handleChange() {
          var loc = "";
          var regCode = "";
          for (let i = 0; i < this.uaddr.length; i++) {
            loc += CodeToText[this.uaddr[i]];
            regCode =this.uaddr[i]
          }
          console.log(loc);
          console.log(regCode);
          this.user.uaddr = loc+","+regCode;
        },
        //行政区使用结束

        imghandleBeforeUpload(file,type,vm) {

          return utils.UPLOAD.Upload().handleBeforeUpload(file,type,vm);
        },
        imgHandleChange(file, fileList, type,vm,modelName,name) {
          if(vm.fileList[type].length!=0){
            alert("请先删除再提交")
          }
          return utils.UPLOAD.Upload().handleChange(file, fileList, type,vm,modelName,name);
        },
        imgHandleRemove(type,vm) {

          return utils.UPLOAD.Upload().handleRemove( type,vm);
        },

        handleUpSuccess(response, file, fileList) {
          if (response.code == 200) {
            this.ruleForm.cardUpImg = response.data[0];
            this.imagesUp = response.data;
            this.hideUp = fileList.length == 1;
            this.$message.success(response.msg);
          }
        },

        //视频成功上传的回调
        handleVideoSuccess(response, file, fileList){
          this.videoFlag = false;//隐藏进度条
          this.videoUploadPercent = 0;//进度重置为0
          if (file.status == 'success' ) {
            let videoId = JSON.parse(response.data).fileId;//保存video的id，用于拼接
            this.$api.getPublicFileUrl().then(res => {
              this.publicFileUrl = res.data.data
              this.editForm.videoUrl = this.publicFileUrl + '/' + videoId
              console.log(this.editForm.videoUrl)
            })
          } else {
            this.$message.info("上传失败，请重新上传");
          }
        },
        uploadImg:function(modelName,name){
          //  使用formdata格式
          let formData = new FormData();
          formData.append("model",modelName);//图片上传子目录
          console.log(formData);
          console.log(this.formData.files[name]);
          if (this.formData.files[name]) {
            formData.append("file", this.formData.files[name]);
          }
          console.log("----------------------------");
          console.log(formData);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          /*api.setDataFromAxios("/api/uploadImg",formData,"post",{headers: { "Content-Type": "multipart/form-data" }})
            .then(dto=>{alert(dto.msg);
              if(dto.code==200){
                this.user.uimg = dto.obj;
              }
            });*///.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },

        setDataFromAxios:function(){
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/liveAdmin/eventRecords",this.eventRecords,"post")
            .then(res=>{
              console.log(this.eventRecords)
              this.$message.success(this.eventRecords.ename+"增加成功")
            });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
      },
      computed:{
        getUploadUrl(){
          return     "/api/uploadImg";
        }

      },
      //监听
      watch: {
        addDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.addDialogIsShow = newValue;
        }
      }
    }
</script>
<style scoped lang="stylus">
 .userForm
   margin 0 auto
   width 1000px
   text-align left
</style>
